interface LiveItem {
  id: string
  title: string
  description: string
  cover: string
  status: string
  viewers: number
  duration: string
  host: {
    name: string
    avatar: string
    level: string
  }
  tags: string[]
}

interface ChatMessage {
  id: string
  content: string
  time: string
  user: {
    name: string
    avatar: string
  }
}

Page({
  data: {
    onlineViewers: 256,
    todayLives: 8,
    messageCount: 1247,
    giftCount: 89,
    currentLive: null as LiveItem | null,
    liveList: [] as LiveItem[],
    chatMessages: [] as ChatMessage[],
    inputMessage: '',
    scrollToMessage: ''
  },

  onLoad() {
    console.log('商品分享live页面加载');
    this.fetchLiveData()
    this.initChatMessages()
  },

  onShow() {
    
  },

  // 获取直播数据
  fetchLiveData() {
    // 模拟API调用
    setTimeout(() => {
      const mockCurrentLive: LiveItem = {
        id: '1',
        title: '稀有装备拍卖专场',
        description: '今晚为大家带来稀有装备拍卖，包括龙牙剑、守护之心等顶级装备，欢迎参与竞拍！',
        cover: 'https://via.placeholder.com/400x300/FF6347/FFFFFF?text=直播封面',
        status: '直播中',
        viewers: 256,
        duration: '02:15:30',
        host: {
          name: '拍卖师小王',
          avatar: 'https://via.placeholder.com/60x60/87CEEB/000000?text=小王',
          level: 'Lv.15'
        },
        tags: ['拍卖', '稀有装备', '实时竞拍']
      }

      const mockLiveList: LiveItem[] = [
        {
          id: '2',
          title: '新手装备推荐',
          description: '为新手玩家推荐性价比高的装备搭配',
          cover: 'https://via.placeholder.com/400x300/32CD32/FFFFFF?text=新手装备',
          status: '直播中',
          viewers: 128,
          duration: '01:30:15',
          host: {
            name: '装备达人',
            avatar: 'https://via.placeholder.com/60x60/FFD700/000000?text=达人',
            level: 'Lv.12'
          },
          tags: ['新手', '装备推荐', '攻略']
        },
        {
          id: '3',
          title: '宠物培养技巧',
          description: '分享宠物培养和技能搭配的技巧',
          cover: 'https://via.placeholder.com/400x300/9370DB/FFFFFF?text=宠物培养',
          status: '直播中',
          viewers: 89,
          duration: '00:45:20',
          host: {
            name: '宠物专家',
            avatar: 'https://via.placeholder.com/60x60/FF69B4/FFFFFF?text=专家',
            level: 'Lv.18'
          },
          tags: ['宠物', '培养', '技能']
        }
      ]

      this.setData({
        currentLive: mockCurrentLive,
        liveList: mockLiveList
      })
    }, 1000)
  },

  // 初始化聊天消息
  initChatMessages() {
    const mockMessages: ChatMessage[] = [
      {
        id: '1',
        content: '主播好！',
        time: '14:30',
        user: {
          name: '玩家001',
          avatar: 'https://via.placeholder.com/50x50/FF6B6B/FFFFFF?text=001'
        }
      },
      {
        id: '2',
        content: '龙牙剑多少钱起拍？',
        time: '14:31',
        user: {
          name: '战士玩家',
          avatar: 'https://via.placeholder.com/50x50/4ECDC4/FFFFFF?text=战士'
        }
      },
      {
        id: '3',
        content: '这个装备太强了！',
        time: '14:32',
        user: {
          name: '魔法师',
          avatar: 'https://via.placeholder.com/50x50/45B7D1/FFFFFF?text=法师'
        }
      },
      {
        id: '4',
        content: '主播能介绍一下装备属性吗？',
        time: '14:33',
        user: {
          name: '新手玩家',
          avatar: 'https://via.placeholder.com/50x50/96CEB4/FFFFFF?text=新手'
        }
      },
      {
        id: '5',
        content: '这个价格很合理！',
        time: '14:34',
        user: {
          name: '老玩家',
          avatar: 'https://via.placeholder.com/50x50/FFEAA7/000000?text=老玩家'
        }
      }
    ]

    this.setData({
      chatMessages: mockMessages,
      scrollToMessage: `msg-${mockMessages[mockMessages.length - 1].id}`
    })
  },

  // 进入直播
  joinLive() {
    wx.showToast({
      title: '进入直播功能开发中',
      icon: 'none',
      duration: 2000
    })
  },

  // 关注主播
  followHost() {
    wx.showToast({
      title: '已关注主播',
      icon: 'success',
      duration: 2000
    })
  },

  // 跳转到直播
  goToLive(e: any) {
    const live = e.currentTarget.dataset.live
    wx.showToast({
      title: `进入${live.title}`,
      icon: 'none',
      duration: 2000
    })
  },

  // 输入框变化
  onInputChange(e: any) {
    this.setData({
      inputMessage: e.detail.value
    })
  },

  // 发送消息
  sendMessage() {
    if (!this.data.inputMessage.trim()) {
      wx.showToast({
        title: '请输入消息内容',
        icon: 'none',
        duration: 1500
      })
      return
    }

    const newMessage: ChatMessage = {
      id: Date.now().toString(),
      content: this.data.inputMessage,
      time: new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
      user: {
        name: '我',
        avatar: 'https://via.placeholder.com/50x50/FF6B6B/FFFFFF?text=我'
      }
    }

    const newMessages = [...this.data.chatMessages, newMessage]
    
    this.setData({
      chatMessages: newMessages,
      inputMessage: '',
      messageCount: this.data.messageCount + 1,
      scrollToMessage: `msg-${newMessage.id}`
    })

    // 模拟其他用户回复
    setTimeout(() => {
      const replyMessage: ChatMessage = {
        id: (Date.now() + 1).toString(),
        content: '收到！',
        time: new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
        user: {
          name: '拍卖师小王',
          avatar: 'https://via.placeholder.com/50x50/87CEEB/000000?text=小王'
        }
      }

      const updatedMessages = [...newMessages, replyMessage]
      this.setData({
        chatMessages: updatedMessages,
        messageCount: this.data.messageCount + 2,
        scrollToMessage: `msg-${replyMessage.id}`
      })
    }, 1000)
  },

  // 开始直播
  startLive() {
    wx.showModal({
      title: '开始直播',
      content: '开始直播功能开发中，敬请期待！',
      showCancel: false,
      confirmText: '知道了'
    })
  }
}); 